package easier.ali.login;

import android.content.Context;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;

public class OneKey {

    //private static final String TAG = "OneKeyV2";

    private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
    private TokenResultListener mTokenResultListener;

    public OneKey( Context context, String secretInfo ) {

        sdkInit( context, secretInfo );
    }

    private void sdkInit( Context context, String secretInfo ) {

        mTokenResultListener = new TokenResultListener() {
            @Override
            public void onTokenSuccess( String s ) {

                hideLoadingDialog();
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson( s );
                    if ( ResultCode.CODE_START_AUTHPAGE_SUCCESS.equals( tokenRet.getCode() ) ) {
                        //Log.i( "TAG", "唤起授权页成功：" + s );
                    }

                    if ( ResultCode.CODE_SUCCESS.equals( tokenRet.getCode() ) ) {
                        //Log.i( "TAG", "获取token成功：" + s );
                        onSuccess( tokenRet.getToken() );
                        mPhoneNumberAuthHelper.setAuthListener( null );
                    }
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTokenFailed( String s ) {

                //Log.e( TAG, "获取token失败：" + s );
                hideLoadingDialog();
                mPhoneNumberAuthHelper.hideLoginLoading();
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson( s );
                    onFailed( tokenRet );
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
                mPhoneNumberAuthHelper.setAuthListener( null );
            }
        };
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance( context, mTokenResultListener );
        mPhoneNumberAuthHelper.getReporter().setLoggerEnable( true );
        mPhoneNumberAuthHelper.setAuthSDKInfo( secretInfo );
    }

    /**
     * 进入app就需要登录的场景使用
     */
    public void oneKeyLogin( Context context ) {

        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance( context.getApplicationContext(), mTokenResultListener );
        mPhoneNumberAuthHelper.checkEnvAvailable( PhoneNumberAuthHelper.SERVICE_TYPE_LOGIN );
        configAuthPage();
        getLoginToken( context, 5000 );
    }

    /**
     * 拉起授权页
     *
     * @param timeout 超时时间
     */
    private void getLoginToken( Context context, int timeout ) {

        mPhoneNumberAuthHelper.getLoginToken( context, timeout );
        showLoadingDialog();
    }

    protected void configAuthPage() {

    }

    protected void showLoadingDialog() {

    }

    protected void hideLoadingDialog() {

    }

    protected void onFailed( TokenRet tokenRet ) {

        /*String code = tokenRet.getCode();
        String msg = tokenRet.getMsg();

        Log.e( TAG, "onFailed: " + code + " " + msg );*/
    }

    protected void onSuccess( final String token ) {

        /*Log.i( TAG, "onSuccess: " + token );*/
    }
}
